package com.hhs.storage.bean;

import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableName;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@Data
@ToString
@TableName("jxc_menu")
public class JxcMenu extends Model<JxcMenu> {

    private static final long serialVersionUID = 1L;

    @TableId("id_")
    private String id;
    @TableField("text_")
    private String text;
    @TableField("icon_")
    private String icon;
    @TableField("url_")
    private String url;
    @TableField("pid_")
    private String pid;
    @TableField("flag_")
    private String flag;
    @TableField("order_")
    private int order;

    @TableField(exist = false)//不是数据库的字段
    private List<JxcMenu> menus;


    @Override
    protected Serializable pkVal() {
        return this.id;
    }

    //排序
    private static Comparator<JxcMenu> order() {
        Comparator<JxcMenu> comparator = new Comparator<JxcMenu>() {
            @Override
            public int compare(JxcMenu o1, JxcMenu o2) {
                if (o1.getOrder() != o2.getOrder()) {
                    return o1.getOrder() - o2.getOrder();
                }
                return 0;
            }
        };

        return comparator;
    }

    private static List<JxcMenu> getChild(String id, List<JxcMenu> allMenu) {
        //子菜单    
        List<JxcMenu> childList = new ArrayList<JxcMenu>();
        for (JxcMenu nav : allMenu) {// 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较     
            //相等说明：为该根节点的子节点。      
            if (id.equals(nav.getPid())) {
                childList.add(nav);
            }
        }
        //递归   
        for (JxcMenu nav : childList) {
            nav.setMenus(getChild(nav.getId(), allMenu));
        }
        Collections.sort(childList, order());//排序   
        //如果节点下没有子节点，返回一个空List（递归退出）  
        if (childList.size() == 0) {
            return new ArrayList<JxcMenu>();
        }
        return childList;
    }

    /**
     * 菜单格式化
     * @param allMenu
     * @return
     * @throws IOException
     */
    public static List<JxcMenu> formatMenu(List<JxcMenu> allMenu) throws IOException {
             //获取一级菜单  
            List<JxcMenu> rootMenu = new ArrayList<JxcMenu>();
            for (JxcMenu nav : allMenu) {
                if (StringUtils.isEmpty(nav.getPid()) || "0".equals(nav.getPid())) {//父节点是0的，为根节点。        
                    rootMenu.add(nav);
                }
            }
            Collections.sort(rootMenu, order());
            //为一级菜单设置子菜单，getClild是递归调用的    
            for (JxcMenu nav : rootMenu) {
                List<JxcMenu> childList = getChild(nav.getId(), allMenu);
                nav.setMenus(childList);
            }
            return rootMenu;
    }
    public static void main(String[] args) {
        String strs[] = new String[]{"a", "b", "c"};
        System.out.println(strs);
    }

}
